package com.yuke.cloud.common.core.support;

import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.dto.GroupInfoDto;
import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.base.exception.BusinessException;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.ThreadLocalMap;
import com.yuke.cloud.common.util.idgenerator.IdWorker;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

//import com.yuke.cloud.common.zk.generator.IncrementIdGenerator;
//import com.yuke.cloud.common.zk.generator.UniqueIdGenerator;

/**
 * The class Base controller.
 *
 * @author
 */
public class BaseController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Value("${yuke.IdWorker.workerId}")
	private Integer workerId;

	@Value("${yuke.IdWorker.datacenterId}")
	private Integer datacenterId;

	@Resource
	private RedisTemplate<String,Object> redisTemplate;


	/**
	 * Gets login auth dto.
	 *
	 * @return the login auth dto
	 */
	protected LoginAuthDto getLoginAuthDto() {
		// todo 20181101 mod by wg 修改成模拟数据，暂时测试用，生产环境后需要还原
		LoginAuthDto loginAuthDto = (LoginAuthDto) ThreadLocalMap.get(GlobalConstant.Sys.TOKEN_AUTH_DTO);

		if (PublicUtil.isEmpty(loginAuthDto)) {
			throw new BusinessException(ErrorCodeEnum.UAC10011041);
		}
		return loginAuthDto;

//		LoginAuthDto loginAuthDto = new LoginAuthDto();
//		loginAuthDto.setUserId(1L);
////		loginAuthDto.setUserId(165491672011112448L);
//		loginAuthDto.setLoginName("admin");
////		loginAuthDto.setLoginName("woshi");
//		loginAuthDto.setGroupId(1L);
//
//		return loginAuthDto;
	}

	/**
	 * Handle result wrapper.
	 *
	 * @param <T>    the type parameter
	 * @param result the result
	 *
	 * @return the wrapper
	 */
	protected <T> Wrapper<T> handleResult(T result) {
		boolean flag = isFlag(result);

		if (flag) {
			return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", result);
		} else {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "操作失败", result);
		}
	}

	/**
	 * Handle result wrapper.
	 *
	 * @param <E>      the type parameter
	 * @param result   the result
	 * @param errorMsg the error msg
	 *
	 * @return the wrapper
	 */
	protected <E> Wrapper<E> handleResult(E result, String errorMsg) {
		boolean flag = isFlag(result);

		if (flag) {
			return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", result);
		} else {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, errorMsg, result);
		}
	}

	private boolean isFlag(Object result) {
		boolean flag;
		if (result instanceof Integer) {
			flag = (Integer) result > 0;
		} else if (result instanceof Boolean) {
			flag = (Boolean) result;
		} else {
			flag = PublicUtil.isNotEmpty(result);
		}
		return flag;
	}

	protected long generateId() {
		//return UniqueIdGenerator.getInstance(IncrementIdGenerator.getServiceId()).nextId();
		IdWorker idWorker = new IdWorker(workerId, datacenterId);
		return idWorker.nextId();
	}

	protected GroupInfoDto getGroupInfoDto() {
		GroupInfoDto groupInfoDto = (GroupInfoDto)redisTemplate.opsForValue().get(GlobalConstant.Sys.ROLE_DEPARTMENT_ADMIN+getLoginAuthDto().getUserId());
		System.out.println("GroupInfoDto值？redisTemplate.opsForValue().get="+groupInfoDto);

		if (PublicUtil.isNotEmpty(groupInfoDto)) {
			ThreadLocalMap.put(GlobalConstant.Sys.ROLE_DEPARTMENT_ADMIN,groupInfoDto);
		}

		return groupInfoDto;

	}
}
  